/*
 * Copyright 2011 Impetus Infotech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.impetus.teamplan.mb;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import com.impetus.teamplan.Constants;
import com.impetus.teamplan.dao.TeamPlanDao;
import com.impetus.teamplan.dao.TeamPlanDaoImpl;
import com.impetus.teamplan.entity.Member;
import com.impetus.teamplan.utils.KunderaUtils;
import com.impetus.teamplan.web.WebUtils;

/**
 * @author amresh.singh
 *
 */
public class LoginBean {
	private String memberId;
	private String password;
	/**
	 * @return the memberId
	 */
	public String getMemberId() {
		return memberId;
	}
	/**
	 * @param memberId the memberId to set
	 */
	public void setMemberId(String memberId) {
		this.memberId = memberId;
	}
	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}
	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}
	
	
	
	public String authenticate() {
		String outcome = "";
		String memberId = getMemberId();
		String password = getPassword();
		

		HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);  
		
		
		//Validates Parameters
		if(WebUtils.isNullOrBlank(memberId)) {
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Please enter your Member ID"));			
			outcome = Constants.OUTCOME_FAILED;
		}
		
		if(WebUtils.isNullOrBlank(password)) {
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Please enter your password"));
			outcome = Constants.OUTCOME_FAILED;
		}		
		
		if(!WebUtils.isNullOrBlank(outcome)) {
			return outcome;
		} else {
			System.out.println("Member: " + memberId + " is trying to login with password " + password);
			TeamPlanDao teamPlanDao = new TeamPlanDaoImpl();
			Member member = teamPlanDao.getMemberById(memberId);
			System.out.println("Found Member " + member + " for " + memberId);
			
			
			
			if(member == null || ! password.equals(member.getPassword())) {
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Login Failed, Try again"));
				outcome = Constants.OUTCOME_FAILED;
								
			} else {
				session.setAttribute(Constants.LOGGED_IN_MEMBER, member);
				outcome = Constants.OUTCOME_SUCCESSFUL;
			}	
			
			return outcome;
		}
	}
	
	public String addAdminUser() {
		TeamPlanDao teamPlanDao = new TeamPlanDaoImpl();
		try {
			teamPlanDao.addMember();
			List<Member> members = teamPlanDao.getAllMembers();
			return Constants.OUTCOME_SUCCESSFUL;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	
	public String logOff() {
		HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);
		session.invalidate();					//Invalidate session		
		KunderaUtils.destroyEntityManager();	//Destroy Entity Manager and factory
		
		//TODO: Stop cassandra server if started
		
		return "loggedOff";
	}
 	
}
